namespace CSharpPro._1027;

using System;

public interface IUserService
{
    bool AddUser(string userName);
    bool DeleteUser(int userId);
    string UpdateUser(int userId, string newUserName);
    string GetUserById(int userId);
}

public class UserServiceImpl : IUserService
{
    private Dictionary<int, string> _users = new Dictionary<int, string>();
    
    public bool AddUser(string userName)
    {
        if (string.IsNullOrEmpty(userName))
            return false;
        int newUserId = _users.Count + 1;
        _users.Add(newUserId, userName);
        Console.WriteLine($"成功新增用户：ID={newUserId}，用户名={userName}");
        return true;
    }
    
    public bool DeleteUser(int userId)
    {
        if (_users.ContainsKey(userId))
        {
            string deletedUserName = _users[userId];
            _users.Remove(userId);
            Console.WriteLine($"成功删除用户：ID={userId}，用户名={deletedUserName}");
            return true;
        }
        Console.WriteLine($"删除失败：未找到ID={userId}的用户");
        return false;
    }
    
    public string UpdateUser(int userId, string newUserName)
    {
        if (string.IsNullOrEmpty(newUserName) || !_users.ContainsKey(userId))
            return "修改失败：参数无效或用户不存在";
        
        string oldUserName = _users[userId];
        _users[userId] = newUserName;
        Console.WriteLine($"成功修改用户：ID={userId}，旧用户名={oldUserName}→新用户名={newUserName}");
        return newUserName;
    }
    
    public string GetUserById(int userId)
    {
        if (_users.ContainsKey(userId))
            return $"查询结果：ID={userId}，用户名={_users[userId]}";
        return $"查询失败：未找到ID={userId}的用户";
    }
}

